package com.donovosoft.api.common;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import oauth.signpost.OAuthConsumer;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.signature.SignatureMethod;

import com.donovosoft.api.NetSender;
import com.donovosoft.api.exceptions.AuthenticationException;
import com.donovosoft.api.exceptions.PathNotFound;

public class OAuthClient extends NetSender {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7242629048015035524L;
	private String consumerKey;
	private String consumerSecret;
	private String accessToken;
	private String tokenSecret;
	private OAuthConsumer consumer = null;
	private int lastResponseCode;

	@Override
	public boolean login() throws AuthenticationException {
		consumer = new DefaultOAuthConsumer(this.consumerKey,
				this.consumerSecret, SignatureMethod.HMAC_SHA1);
		consumer.setTokenWithSecret(this.accessToken, this.tokenSecret);
		if (consumer == null) {
			throw new AuthenticationException("Cannont authenticate OAUTH");
		}
		return true;
	}

	public String consume(String url) throws MalformedURLException {
		return this.consume(new URL(url));
	}

	public String consume(URL url) {
		HttpURLConnection request;
		String response = null;
		try {
			request = (HttpURLConnection) url.openConnection();
			consumer.sign(request);
			request.connect();
			this.lastResponseCode = request.getResponseCode();
			response = request.getResponseMessage();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (OAuthMessageSignerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (OAuthExpectationFailedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return response;
	}

	public OAuthConsumer getConsumer() {
		return consumer;
	}

	public int getLastResponseCode() {
		return lastResponseCode;
	}

	@Override
	public void changeDir(String path) throws PathNotFound {
		// TODO Auto-generated method stub
	}

	@Override
	public String[] listFiles() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean deleteFile(String name) throws IOException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean makeDir(String path) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean deleteDir(String path) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public long getModificationTime(String path) throws PathNotFound {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String currentDir() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getAccessToken() {
		return accessToken;
	}

	public void setAccessToken(String accessToken) {
		this.accessToken = accessToken;
	}

	public String getTokenSecret() {
		return tokenSecret;
	}

	public void setTokenSecret(String tokenSecret) {
		this.tokenSecret = tokenSecret;
	}

	@Override
	public boolean putFile(File stream) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public OutputStream getFile(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean logout() {
		// TODO Auto-generated method stub
		return false;
	}

	public String getConsumerKey() {
		return consumerKey;
	}

	public void setConsumerKey(String consumerKey) {
		this.consumerKey = consumerKey;
	}

	public String getConsumerSecret() {
		return consumerSecret;
	}

	public void setConsumerSecret(String consumerSecret) {
		this.consumerSecret = consumerSecret;
	}

}
